package com.agu.code;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Component
public class CodeConfig {

	public static Map<String, Object> maps = new HashMap<>();

	public static CodeMap codeMap = new CodeMap();

	public static List<HashMap<String, String>> getTemplates(String name) {

		HashMap<String, List<HashMap<String, String>>> templates = codeMap.getTemplates();

		if (templates != null) {

			return templates.get(name);

		}
		return null;
	}

	@PostConstruct
	private void init() {
		try {
			URL url = getClass().getResource("/code.json");

			File file = new File(url.toURI());
			if (file.exists()) {
				String json = FileUtils.readFileToString(new File(url.toURI()), "utf-8");
				codeMap = JSON.parseObject(json, CodeMap.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
